// 简化的ArkTS语法验证文件
// 用于验证Index.ets中的关键语法结构

// 1. 基本状态变量声明（模拟@State）
let score: number = 0
let gameStarted: boolean = false
let gameOver: boolean = false
let snakeX: number = 50
let snakeY: number = 50
let foodX: number = 150
let foodY: number = 150
let moveDirection: string = 'right'

// 2. 游戏逻辑函数
function startGame(): void {
  gameStarted = true
  gameOver = false
  score = 0
  snakeX = 50
  snakeY = 50
  moveDirection = 'right'
  generateFood()
}

function endGame(): void {
  gameStarted = false
  gameOver = true
}

function updateGame(): void {
  let newX = snakeX
  let newY = snakeY
  
  switch (moveDirection) {
    case 'up':
      newY -= 10
      break
    case 'down':
      newY += 10
      break
    case 'left':
      newX -= 10
      break
    case 'right':
      newX += 10
      break
  }
  
  // 边界检查
  if (newX < 0 || newX >= 300 - 10 || newY < 0 || newY >= 300 - 10) {
    endGame()
    return
  }
  
  snakeX = newX
  snakeY = newY
  
  // 食物检查
  if (Math.abs(snakeX - foodX) < 10 && Math.abs(snakeY - foodY) < 10) {
    score += 10
    generateFood()
  }
}

function generateFood(): void {
  foodX = Math.floor(Math.random() * (300 - 10))
  foodY = Math.floor(Math.random() * (300 - 10))
}

function changeDirection(newDirection: string): void {
  if ((moveDirection === 'up' && newDirection === 'down') ||
      (moveDirection === 'down' && newDirection === 'up') ||
      (moveDirection === 'left' && newDirection === 'right') ||
      (moveDirection === 'right' && newDirection === 'left')) {
    return
  }
  moveDirection = newDirection
}

// 3. 验证函数调用
startGame()
updateGame()
changeDirection('up')
endGame()

console.log('语法检查通过！')